home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / System7 tools / Frontier / Frontier SDK 2.1 / Toolkits / Applet Toolkit / appletfrontier.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-11  |  7.9 KB  |  362 lines  |  [TEXT/KAHL]

  1.  
  2. /*© Copyright 1988-1992 UserLand Software, Inc.  All Rights Reserved.*/
  3.  
  4.  
  5. #include <iac.h>
  6. #include "appletdefs.h"
  7. #include "appletstrings.h"
  8. #include "appletmemory.h"
  9. #include "appletfrontier.h"
  10.  
  11.  
  12.  
  13.  
  14. typedef struct versionRecord {
  15.  
  16.     unsigned short majorRev: 8; 
  17.     
  18.     unsigned short minorRev: 4;    
  19.     
  20.     unsigned short bugFixRev: 4; 
  21.     
  22.     unsigned short reserved: 15;
  23.     
  24.     unsigned short flFrontier: 1; /*true if it's Frontier, false if it's Runtime*/
  25.     } versionRecord;
  26.  
  27.  
  28. #define frag1 "\pif not defined ("
  29.  
  30. #define frag2 "\p) {speaker.beep (); return;}; Frontier.bringToFront (); edit (@"
  31.  
  32. #define frag3 "\p)"
  33.  
  34.     
  35. boolean FrontierOpenObject (bigstring objectaddress) {
  36.     
  37.     bigstring script, returns;
  38.     
  39.     setstringlength (script, 0);
  40.     
  41.     pushstring (frag1, script);
  42.     
  43.     pushstring (objectaddress, script);    
  44.     
  45.     pushstring (frag2, script);
  46.     
  47.     pushstring (objectaddress, script);
  48.     
  49.     pushstring (frag3, script);
  50.     
  51.     FrontierDoScript (script, returns);
  52.     } /*FrontierOpenObject*/
  53.     
  54.     
  55. boolean FrontierDoScript (bigstring script, bigstring returns) {
  56.     
  57.     /*
  58.     Send a Do Script message to Frontier. The first parameter contains a short
  59.     script to be run. The second parameter is the string that Frontier returned 
  60.     as the value generated by running the script. 
  61.     
  62.     Returns true if we were able to send the message to Frontier, and Frontier
  63.     was able to compile and run the script and Frontier replied with a returned
  64.     value. FrontierDoScript returns false if Frontier isn't running, or if the
  65.     script didn't compile or if there was a communications error.
  66.     
  67.     Frontier is not limited to running 255-character scripts or returning 
  68.     255-character returned values. This routine can easily be enhanced to handle 
  69.     larger scripts and returned values. 
  70.     
  71.     11/6/91 DW: Set return string to "IAC Error" if we failed to create an Apple
  72.     event descriptor or if the send failed. We're not suggesting that your
  73.     error messages should be so brief, rather they should be custom-fit to the
  74.     appropriate audience. Here, we want to show you how to locate errors relating
  75.     to the IAC channel -- either you're low on memory, or the Apple Event Manager
  76.     isn't present, or Frontier isn't running. Watch for this string in FDS's 
  77.     little window...
  78.     
  79.     11/7/91 DW: This code was cribbed from the Frontier Do-Script program and adapted
  80.     to run on top of the IAC Tools library. Use this version of FDS if you're using
  81.     the IAC Tools library, use the original version if you're writing code to run
  82.     directly on top of the Apple Event Manager.
  83.     */
  84.  
  85.     register Boolean flhavereply = false;
  86.     AppleEvent event, reply;
  87.     
  88.     copystring ("\pIAC Error.", returns); /*default return string*/
  89.     
  90.     if (!IACnewverb ('LAND', 'misc', 'dosc', &event))
  91.         return (false);
  92.     
  93.     IACglobals.event = &event;
  94.     
  95.     if (!IACpushstringparam (script, '----'))
  96.         return (false);
  97.         
  98.     if (!IACsendverb (&event, &reply))
  99.         goto error;
  100.     
  101.     flhavereply = true;
  102.     
  103.     IACglobals.reply = &reply;
  104.     
  105.     if (IACiserrorreply (returns)) /*syntax error or runtime error*/
  106.         goto error;
  107.         
  108.     IACglobals.event = &reply; /*get the string from the reply record*/
  109.         
  110.     if (!IACgetstringparam ('----', returns))
  111.         goto error;
  112.     
  113.     AEDisposeDesc (&event);    
  114.     
  115.     AEDisposeDesc (&reply);
  116.     
  117.     return (true);
  118.     
  119.     error:
  120.     
  121.     AEDisposeDesc (&event);    
  122.     
  123.     if (flhavereply)
  124.         AEDisposeDesc (&reply);
  125.     
  126.     return (false);
  127.     } /*FrontierDoScript*/
  128.     
  129.     
  130. boolean FrontierDoHandleScript (Handle hscript, boolean flfast, boolean flgetreturn, bigstring errorstring, Handle *hreturns) {
  131.     
  132.     AppleEvent event, reply;
  133.     boolean fl = true;
  134.     
  135.     setstringlength (errorstring, 0);
  136.     
  137.     if (flfast) {
  138.     
  139.         if (!IACnewsystemverb ('fast', 'dosc', &event))
  140.             return (false);
  141.         }
  142.     else {
  143.     
  144.         if (!IACnewverb ('LAND', 'misc', 'dosc', &event))
  145.             return (false);
  146.         }
  147.     
  148.     IACglobals.event = &event;
  149.     
  150.     if (!IACpushtextparam (hscript, '----'))
  151.         return (false);
  152.         
  153.     if (!IACsendverb (&event, &reply))
  154.         return (false);
  155.     
  156.     IACglobals.reply = &reply;
  157.     
  158.     if (IACiserrorreply (errorstring)) {
  159.         
  160.         fl = false;
  161.         
  162.         goto exit;
  163.         }
  164.         
  165.     if (flgetreturn) {
  166.     
  167.         IACglobals.event = &reply; /*get the string from the reply record*/
  168.         
  169.         fl = IACgettextparam ('----', hreturns);
  170.         
  171.         if (fl)
  172.             fl = copyhandle (*hreturns, hreturns);
  173.         }
  174.     
  175.     exit:
  176.     
  177.     AEDisposeDesc (&event);    
  178.     
  179.     AEDisposeDesc (&reply);
  180.     
  181.     return (fl);
  182.     } /*FrontierDoHandleScript*/
  183.  
  184.  
  185. boolean FrontierFastDoScript (bigstring bsscript, boolean flgetreturn, bigstring errorstring, bigstring bsreply) {
  186.     
  187.     /*
  188.     send a fast system-level script to Frontier and get back a value if flgetreturn is
  189.     true. we only send the message the fast way if there is a system handler installed.
  190.     
  191.     use this for scripts that don't open windows or display things in Frontier. it's
  192.     good for setting and getting string values in the object database.
  193.     */
  194.     
  195.     AppleEvent event, reply;
  196.     boolean fl = true;
  197.     
  198.     setstringlength (errorstring, 0);
  199.     
  200.     if (IAChandlerinstalled ('fast', 'dosc', true)) { 
  201.     
  202.         if (!IACnewsystemverb ('fast', 'dosc', &event))
  203.             return (false);
  204.         }
  205.     else {
  206.     
  207.         if (!IACnewverb ('LAND', 'misc', 'dosc', &event))
  208.             return (false);
  209.         }
  210.     
  211.     IACglobals.event = &event;
  212.     
  213.     if (!IACpushstringparam (bsscript, '----'))
  214.         return (false);
  215.         
  216.     if (!IACsendverb (&event, &reply))
  217.         return (false);
  218.     
  219.     IACglobals.reply = &reply;
  220.     
  221.     if (IACiserrorreply (errorstring)) {
  222.         
  223.         fl = false;
  224.         
  225.         goto exit;
  226.         }
  227.         
  228.     if (flgetreturn) {
  229.     
  230.         IACglobals.event = &reply; /*get the string from the reply record*/
  231.         
  232.         fl = IACgetstringparam ('----', bsreply);
  233.         }
  234.     
  235.     exit:
  236.     
  237.     AEDisposeDesc (&event);    
  238.     
  239.     AEDisposeDesc (&reply);
  240.     
  241.     return (fl);
  242.     } /*FrontierFastDoScript*/
  243.  
  244.  
  245. boolean FrontierIsRunning (void) {
  246.     
  247.     /*
  248.     return true if the server application is running. 
  249.     */
  250.     
  251.     ProcessInfoRec info;
  252.     ProcessSerialNumber psn;
  253.     Str255 bsname;
  254.     FSSpec fss;
  255.     
  256.     info.processInfoLength = sizeof (info);
  257.     
  258.     info.processName = bsname; /*place to store process name*/
  259.     
  260.     info.processAppSpec = &fss; /*place to store process filespec*/
  261.     
  262.     psn.highLongOfPSN = kNoProcess;
  263.     
  264.     psn.lowLongOfPSN = kNoProcess;
  265.     
  266.     while (GetNextProcess (&psn) == noErr) {
  267.         
  268.          info.processInfoLength = sizeof (ProcessInfoRec);
  269.          
  270.         if (GetProcessInformation (&psn, &info) != noErr)
  271.             continue; /*keep going -- ignore error*/
  272.         
  273.         if (info.processSignature == 'LAND')
  274.             return (true);
  275.         } /*while*/
  276.     
  277.     return (false); /*loop completed, no Frontier*/
  278.     } /*FrontierIsRunning*/
  279.     
  280.     
  281. boolean getFrontierVersion (short *majorRev, short *minorRev, short *bugFixRev, boolean *flRuntime) {
  282.     
  283.     /*
  284.     if Frontier isn't running, return false.
  285.     
  286.     if it is, return true with information about the version of Frontier that's running.
  287.     
  288.     About the Apple Event we send...
  289.     
  290.     We call a system event handler, so it's very fast. 
  291.     
  292.     It takes no parameters, and returns a long value. The high word of the long is the version 
  293.     number, packed the same way as the system version is packed into the SysEnvirons record. 
  294.     (8 bits major version, 4 bits minor version, 4 bits revision. The version 2.1.1 would 
  295.     be 0x0211.) The low word contains attributes of the server program. At this point only 
  296.     a single bit is defined: the low order bit is set if Frontier is the server; otherwise, 
  297.     Runtime is the server.
  298.     */
  299.     
  300.     AppleEvent event, reply;
  301.     Boolean flhavereply = false;
  302.     versionRecord x;
  303.     
  304.     long z;
  305.     
  306.     z = sizeof (x);
  307.     
  308.     if (!FrontierIsRunning ())
  309.         return (false);
  310.     
  311.     if (!IAChandlerinstalled ('LAND', 'who?', true)) { /*it's Frontier 1.0, not Runtime*/
  312.         
  313.         *majorRev = 1;
  314.         
  315.         *minorRev = 0;
  316.         
  317.         *bugFixRev = 0;
  318.         
  319.         *flRuntime = false;
  320.         
  321.         return (true);
  322.         }
  323.  
  324.     if (!IACnewsystemverb ('LAND', 'who?', &event))
  325.         return (false);
  326.     
  327.     if (!IACsendverb (&event, &reply))
  328.         goto error;
  329.     
  330.     flhavereply = true;
  331.     
  332.     IACglobals.reply = &reply;
  333.     
  334.     IACglobals.event = &reply; /*get the string from the reply record*/
  335.         
  336.     if (!IACgetlongparam ('----', (long *) &x))
  337.         goto error;
  338.     
  339.     *majorRev = x.majorRev;
  340.     
  341.     *minorRev = x.minorRev;
  342.     
  343.     *bugFixRev = x.bugFixRev;
  344.     
  345.     *flRuntime = !x.flFrontier;
  346.     
  347.     AEDisposeDesc (&event);    
  348.     
  349.     AEDisposeDesc (&reply);
  350.     
  351.     return (true);
  352.     
  353.     error:
  354.     
  355.     AEDisposeDesc (&event);    
  356.     
  357.     if (flhavereply)
  358.         AEDisposeDesc (&reply);
  359.     
  360.     return (false);
  361.     } /*getFrontierVersion*/
  362.